import path from 'path-browserify';
export const getMenuListByRoutes = (list, basePath = '/') => {
  let arr = [];
  for (let i = 0, len = list.length; i < len; i++) {
    let item = list[i];
    if (item.meta && item.meta.hideInMenu) {
      continue;
    }
    if (Array.isArray(item.children) && item.children.length === 1 && !(item?.meta?.showAlways)) {
      item = { ...item.children[0], path: path.join(item.path, item.children[0].path)}
    }
    let obj = {
      icon: (item.meta && item.meta.icon) || '',
      title: (item.meta && item.meta.title) || '',
      path: path.join(basePath, item.path),
      meta: item.meta || {},
      name: item.name
    };
    if (Array.isArray(item.children) && item.children.length > 0) {
      obj.children = getMenuListByRoutes(
        item.children,
        path.join(basePath, item.path)
      );
    }
    arr.push(obj);
  }
  return arr;
};